#include "dialogeditor.h"
#include "ui_dialogeditor.h"
#include <QDebug>
#include <QFile>
#include <QFileDialog>
#include <QSpinBox>
DialogEditor::DialogEditor(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::DialogEditor)
{
    ui->setupUi(this);
    this->setFixedSize(900,600);
    this->setWindowTitle("编辑对话");
    this->setWindowIcon(QIcon(":/res/icon/myGame.png"));
    this->setWindowModality(Qt::ApplicationModal);

    InitColor();
    InitEditor();
}

DialogEditor::~DialogEditor()
{
    delete ui;
}

void DialogEditor::InitColor()
{
    colorDialog.setWindowModality(Qt::ApplicationModal);
    connect(ui->NameColor,&QPushButton::clicked,this,[=](){
        colorDialog.show();
        selectName = true;
        ui->NameEdit->setTextColor(colorDialog.currentColor());
    });
    connect(ui->DialogColor,&QPushButton::clicked,this,[=](){
        colorDialog.show();
        selectName = false;
        ui->TextEdit->setTextColor(colorDialog.currentColor());
    });

    connect(&colorDialog,&QColorDialog::colorSelected,this,[=](){
        QColor color = colorDialog.currentColor();
        float r = color.red(),g = color.green(),b = color.blue();
        QString qss = "background-color:rgb(" +
                QString::number(r) + "," +
                QString::number(g) + "," +
                QString::number(b) + ");border:none;";
        if(selectName)
        {
            ui->NameColor->setStyleSheet(qss);
        }
        else
        {
            ui->DialogColor->setStyleSheet(qss);
        }
    });
}

void DialogEditor::InitEditor()
{
    connect(ui->NameFontBox,&QFontComboBox::currentFontChanged,[=](const QFont& font){
        tcf.setFontFamily(font.family());
        QStringList sl;
        sl<<font.family();
        tcf.setFontFamilies(sl);
        ui->NameEdit->mergeCurrentCharFormat(tcf);
    });
    void (QSpinBox:: *spinBoxSignal)(int) = &QSpinBox::valueChanged;
    connect(ui->NameFontSize,spinBoxSignal,this,[=](int i){
        ui->NameEdit->setFontPointSize(i);
    });
    connect(ui->DialogFontSize,spinBoxSignal,this,[=](int i){
        ui->TextEdit->setFontPointSize(i);
    });
    connect(ui->DialogFontBox,&QFontComboBox::currentFontChanged,[=](const QFont& font){
        tcf.setFontFamily(font.family());
        QStringList sl;
        sl<<font.family();
        tcf.setFontFamilies(sl);
        ui->TextEdit->mergeCurrentCharFormat(tcf);
    });
    connect(ui->YesBtn,&QPushButton::clicked,this,[=](){
        YesBtn();
        this->hide();
    });
    connect(ui->TextV,spinBoxSignal,this,[=](int i){
        emit EventCenter::Instance().ChangeTextV(i);
    });
}

void DialogEditor::Open(QString &name, QString &text)
{
    ui->NameEdit->setText(name);
    ui->TextEdit->setText(text);
    this->show();
}

void DialogEditor::YesBtn()
{
    QString name = ui->NameEdit->toHtml();
    QString text = ui->TextEdit->toHtml();
    emit EventCenter::Instance().TextEdit(name,text);
    this->hide();
    emit EventCenter::Instance().Modify();
}
